﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;



namespace PBRSystem.Coord
{
    public class PBRBounds2
    {
        #region Variables
        private PBRPoint2 _pMin, _pMax;
        #endregion

        #region Reporter
        public PBRPoint2 PMin { get => _pMin; set => _pMin = value; }
        public PBRPoint2 PMax { get => _pMax; set => _pMax = value; }
        #endregion

        #region Contructor
        public PBRBounds2()
        {
            _pMax = new PBRPoint2(float.PositiveInfinity, float.PositiveInfinity);
            _pMin = new PBRPoint2(0, 0);
        }

        public PBRBounds2(PBRPoint2 p)
        {
            _pMin = new PBRPoint2(p);
            _pMax = new PBRPoint2(p);
        }

        public PBRBounds2(PBRPoint2 p1, PBRPoint2 p2)
        {
            _pMin = new PBRPoint2(p1);
            _pMax = new PBRPoint2(p2);
        }




        #endregion

        #region Operation
        public PBRVector2 Diagonal()
        {
            return _pMax - _pMin;
        }

        public float Area()
        {
            PBRVector2 d = Diagonal();
            return (d.X * d.Y);
        }

        public int MaximumExtent()
        {
            PBRVector2 d = Diagonal();
            if(d.X > d.Y)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
        #endregion

        #region Operator
        public static bool operator == (PBRBounds2 a, PBRBounds2 b)
        {
            return b.PMin == a.PMin && a.PMax == b.PMin;
        }

        public static bool operator !=(PBRBounds2 a, PBRBounds2 b)
        {
            return b.PMin != a.PMin || a.PMax != b.PMin;
        }

        public void BoundingSphere(PBRPoint2 p, float rad)
        {

        }

        public bool Overlaps(PBRBounds2 b1, PBRBounds2 b2)
        {
            bool x = (b1.PMax.X >= b2.PMin.X) && (b1.PMin.X <= b2.PMax.X);
            bool y = (b1.PMax.Y >= b2.PMin.Y) && (b1.PMin.Y <= b2.PMax.Y);

            return x && y;
        }

        public bool Inside(PBRPoint2 p, PBRBounds2 b)
        {
            return (p.X >= b.PMin.X && p.X <= b.PMax.X && p.Y >= b.PMin.Y && p.Y <= b.PMax.Y);
        }

        public bool InsideExclusive(PBRPoint2 p, PBRBounds2 b)
        {
            return (p.X >= b.PMin.X && p.X < b.PMax.X && p.Y >= b.PMin.Y && p.Y < b.PMax.Y);
        }

        public PBRPoint2 Lerp(PBRPoint2 t)
        {
            return PBRPoint2.Lerp(t.X, PMin, PMax); 
        }
        #endregion

        #region Override

        public override bool Equals(object obj)
        {
            if(!(obj is PBRBounds2))
            {
                return false;
            }
            PBRBounds2 b = (PBRBounds2)obj;
            return _pMax == b.PMax && _pMin == b.PMin;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        #endregion
    }
}


